﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace INSTA.Samples.EntityFramework.WinUI
{
    public partial class frmCreateDepartment : Form
    {
        //Create an ObjectContext instance based on SchoolEntity
        private SchoolSampleEntities _schoolContext;

        public frmCreateDepartment()
        {
            InitializeComponent();
        }

        private void frmCreateDepartment_Load(object sender, EventArgs e)
        {
            //Initialize the ObjectContext
            _schoolContext = new SchoolSampleEntities();

            // Define a query that returns all Department  
            // objects and course objects, ordered by name.
            //var departmentQuery = from department in _schoolContext.Departments.Include("Courses")
            //                      orderby department.Name
            //                     select department;



            var administrators = from person in _schoolContext.People
                                 select person;


            var administrators2 = from person in _schoolContext.People
                                 select new
                                            {
                                                Name = person.FirstName + " " + person.LastName,
                                                Id = person.PersonID,
                                                
                                            };



            try
            {
                // Bind the ComboBox control to the query, 
                // which is executed during data binding.
                // To prevent the query from being executed multiple times during binding, 
                // it is recommended to bind controls to the result of the Execute method. 
                cboAdministrators.DisplayMember  = "Name";
                cboAdministrators.ValueMember    = "Id";
                cboAdministrators.DataSource = administrators2.ToList();



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var department = new Department()
                                 {
                                     Administrator  = (int)cboAdministrators.SelectedValue,
                                     Budget         = Convert.ToDecimal(txtBudget.Text),
                                     Name           = txtName.Text,
                                     StartDate      = dtpStartDate.Value,
                                 };

            _schoolContext.Departments.AddObject(department);
            _schoolContext.SaveChanges();
        }

       
    }
}
